package tongwentongshu.com.app.db;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.tencent.qcloud.presentation.event.MessageEvent;
import com.tencent.qcloud.tlslibrary.service.TlsBusiness;

import tongwentongshu.com.app.App;
import tongwentongshu.com.app.activity.LoginActivity;
import tongwentongshu.com.app.activityim.UserInfo;
import tongwentongshu.com.app.bean.LoginUser;
import tongwentongshu.com.app.utils.SpUtils;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/6.
 */

public class Cache {
    public static final String READ_ING = "1";
    public static final String READ_FREE = "2";
    public static final String READ_APPOINTMENT = "3";

    public static final String HOME_FREE = "1";//首页空闲
    public static final String READ_RESERVE = "2";//首页被预约
    public static final String READ_READING = "3";//首页在读

    public static final String READING_KEY = "reading";//我的读书key
    public static final String PER_RESERVE = "1";//个人中心我预约的
    public static final String PER_READING = "2";//个人中心在读
    public static final String PER_HAVE_READ = "4";//个人中心读过的
    public static final String PER_CANCEL_RESERVE = "7";//个人中心取消预约的
    public static final String PER_FREE = "5";//个人中心空闲
    public static final String PER_COVER_RESERVE = "6";//个人中心被预约的

    public static final String RETRIEVAL_KEY = "retrieval";//找回密码的key
    public static final String RETRIEVAL_VERIFICATION = "1";//发送验证码
    public static final String RETRIEVAL_RESET = "2";//重置密码

    public static final String REGISTER_KEY = "register";//注册的key
    public static final String REGISTER_VERIFICATION = "1";//手机号验证
    public static final String REGISTER_PASSWORD = "2";//设置密码

    private static LoginUser user;
    public final static String USER_NAME = "user_name";
    public final static String USER_PASSWORD = "user_password";
    public final static String USER_ID = "user_id";
    public final static String INDEX = "index";

    public final static String PAY_DOPSIT = "dopsit";
    public final static String PAY_DEPOSIT = "deposit";
    public final static String PAY_RECHARGE = "recharge";

    public final static String LAT = "lat";
    public final static String LON = "lon";
    public final static String ADDRESS = "address";

    public static LoginUser getUser() {
        if (user == null) {
            setUser();
        }
        return user;
    }

    public static void setUser() {
        Cache.user = (LoginUser) DataKeeper.readSerializable(
                App.getContext(), LoginUser.class);
        if (Cache.user == null) {
            Cache.user = new LoginUser();
        }
    }

    public static String getToken() {
        if (isNeedLogin()) {
            return "";
        }
        if (user != null) {
            return getUser().getData().getToken();
        }
        return "";
    }

    public static boolean LoginVer(Context mContext) {
        if (isNeedLogin()) {
            ActivityManager activityManager = (ActivityManager) mContext
                    .getSystemService(Context.ACTIVITY_SERVICE);
            String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity
                    .getClassName();
            if (runningActivity.contains("LoginActivity")) {
                return false;
            }
            Intent mIntent = new Intent(mContext, LoginActivity.class);
            mContext.startActivity(mIntent);

        }
        return isNeedLogin();
    }

    public static void clear() {
        user = null;
    }

    public static boolean isNeedLogin() {
        return !getUser().isLogin();
    }

    public static void Logout(Context context) {
        SpUtils.setString(context, Cache.USER_NAME, "");
        SpUtils.setString(context, Cache.USER_PASSWORD, "");
        DataKeeper.clear(context, LoginUser.class);
        getUser().getData().setToken("");
        getUser().setLogin(false);
        ToastUtil.show(context, "退出登录成功");
        //清空聊天相关讯息
//        TlsBusiness.logout(UserInfo.getInstance().getId());
        UserInfo.getInstance().setId(null);
        MessageEvent.getInstance().clear();

        App.getApplication().exitMall();
    }
}
